package com.hyq.learn.ai.service;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.stereotype.Service;

import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

/**
 * @author heyuqin
 * @date 2025/10/16 16:13
 */
@Service
public class ChatService {
    private final ChatClient chatClient;

    public ChatService(ChatClient.Builder builder) {
        this.chatClient = builder.build();
    }

    public void streamChat(String prompt, OutputStream outputStream) {
//        String content = chatClient.prompt(prompt).call().content();
        for (int i = 0; i < 5; i++) {
            try {
                outputStream.write(("test:" + i + ",").getBytes(StandardCharsets.UTF_8));
                outputStream.flush();
            } catch (Exception ignored) {
            }
            try {
                Thread.sleep(500L);
            } catch (InterruptedException ignored) {
            }
        }
    }
}
